package com.tiny.mirror.service.product.infrastructure.repository;

import com.tiny.mirror.basic.common.code.CommonStatusCode;
import com.tiny.mirror.basic.db.domain.Aggregate;
import com.tiny.mirror.basic.db.domain.AggregateFactory;
import com.tiny.mirror.basic.web.exception.BusinessException;
import com.tiny.mirror.service.product.domain.DomainFactory;
import com.tiny.mirror.service.product.domain.app.App;
import com.tiny.mirror.service.product.domain.app.repository.AppRepository;
import com.tiny.mirror.service.product.infrastructure.entity.db.AppEntity;
import com.tiny.mirror.service.product.infrastructure.repository.db.AppMapper;
import java.util.Objects;
import javax.annotation.Resource;
import org.springframework.stereotype.Repository;

/**
 * @author: chenhao
 * @description:
 * @date: 2023/8/28
 **/
@Repository
public class AppRepositoryImpl implements AppRepository {

  @Resource
  private AppMapper appMapper;

  @Override
  public Aggregate<App> findById(long id) {
    App app = findAppById(id);
    return AggregateFactory.createAggregate(app);
  }

  @Override
  public App findAppById(long id) {
    AppEntity entity = appMapper.selectById(id);
    if (Objects.isNull(entity)) {
      throw new BusinessException(CommonStatusCode.DATA_NOT_EXIST.args("app:" + id));
    }
    return DomainFactory.convertToApp(entity);
  }
}
